package com.xxx.shetuan3.controller;

import com.xxx.shetuan3.common.CommonResult;
import com.xxx.shetuan3.common.CommonResultCode;
import com.xxx.shetuan3.pojo.User;
import com.xxx.shetuan3.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class LoginController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "登陆成功")
    @PostMapping("/loginsuccess")
    public CommonResult<User> longinSuccess(){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username;
        if (principal instanceof UserDetails) {
            username = ((UserDetails)principal).getUsername();
        } else {
            username = principal.toString();
        }
        User user = userService.findUserByAccount(username);
        user.setPassword(null);
        return CommonResult.success(user);
    }
    @ApiOperation("登陆失败")
    @PostMapping("/loginfailed")
    public CommonResult loginFailed(){
        return CommonResult.failed(CommonResultCode.VALIDATE_FAILED);
    }
}
